$(function () {
    var e = require("pokeball/components/modal"),
        o = (require("pokeball/components/overlay"), {
            set: function (e, o, r) {
                var t = JSON.stringify(o),
                    a = new Date;
                a.setDate(a.getDate() + r), document.cookie = e + "=" + encodeURIComponent()(t) + (null == r ? "" : ";expires=" + a.toGMTString())
            }, get: function (e) {
                var o, r;
                if (document.cookie.length > 0 && -1 != (o = document.cookie.indexOf(e + "="))) return o = o + e.length + 1, -1 == (r = document.cookie.indexOf(";", o)) && (r = document.cookie.length), JSON.parse(decodeURIComponent(document.cookie.substring(o, r)));
                return null
            }, del: function (e) {
                var r = new Date;
                r.setTime(r.getTime() - 1);
                var t = o.get(e);
                null != t && (document.cookie = e + "=" + t + ";expires=" + r.toGMTString())
            }
        }),
        r = {
            _available: void 0 !== window.localStorage && null != typeof window.localStorage,
            set: function (e, t) {
                if (r._available) window.localStorage[e] = JSON.stringify(t);
                else {
                    var a = {};
                    a[e] = t;
                    var n = o.get("_LocalStorage");
                    n ? ($.extend(n, a), o.set("_LocalStorage", n, 1)) : o.set("_LocalStorage", a, 1)
                }
            }, get: function (e) {
                if (r._available) {
                    var t = window.localStorage[e];
                    return t ? JSON.parse(t) : t
                }
                var a = o.get("_LocalStorage");
                return a ? a[e] : void 0
            }, del: function (e) {
                if (r._available) window.localStorage.removeItem(e);
                else {
                    var t = o.get("_LocalStorage");
                    t && (delete t[e], o.set("_LocalStorage", t))
                }
            }
        },
        t = {
            showAccountError: function (e) {
                $(".account-form .error-text").addClass("error").find(".text").html(e)
            }, hideAccountError: function () {
                $(".account-form .error-text").removeClass("error").find(".text").html("")
            }, showCAError: function (e) {
                $(".CA-form .error-text").addClass("error").find(".text").html(e)
            }, hideCAError: function () {
                $(".CA-form .error-text").removeClass("error").find(".text").html("")
            }
        },
        a = "/login/sendSMSVerifyCode",
        n = {
            loginType: "password",
            certificateNum: 0,
            bindEvents: function () {
                var e = this;
                $(".login-tab").on("click", function () {
                    var o = $(this).attr("type");
                    $(this).addClass("active").siblings().removeClass("active");
                    var r = $(".form-content[data-type=" + o + "]");
                    r.removeClass("hide").siblings(".form-content").addClass("hide"), "password" === o || "mobile" === o ? e.refreshForm(r) : e.loadingCA(o)
                }), $(".change-login-type").on("click", function () {
                    var o = $(this).attr("type"),
                        r = $(".account-form[data-type=" + o + "]");
                    r.removeClass("hide").siblings(".account-form").addClass("hide"), e.refreshForm(r), $("li.account-tab").attr("type", o)
                }), $(".refresh-CA").on("click", function () {
                    $(".login-tab.CA-tab").attr("type", "CA-loading"), e.loadingCA("CA-loading")
                }), $(".get-captcha").on("click", function () {
                    var e = this;
                    $(this).hasClass("disabled") || $.ajax({
                        url: a + "?phoneNumber=" + $("#mobile-number").val(),
                        method: "GET"
                    }).success(function (o) {
                        var r, a;
                        1 != o ? (t.showAccountError(o || "获取验证码失败，请重试"), $(e).removeClass("hide"), $(".mobile-captcha-wrap .count-down").addClass("hide")) : ($(this).addClass("hide"), $(".mobile-captcha-wrap .count-down").removeClass("hide"), r = 59, a = window.setInterval(function () {
                            r--, $(".count-down .time").html(r), r < 2 && (window.clearInterval(a), $(".mobile-captcha-wrap .count-down").addClass("hide").find(".time").html(59), $(e).removeClass("hide"))
                        }, 1e3))
                    }).error(function () {
                        t.showAccountError("获取验证码失败，请重试"), $(e).removeClass("hide"), $(".mobile-captcha-wrap .count-down").addClass("hide")
                    })
                }), $(".captcha-img").on("click", function () {
                    $(this).attr("src", "login" + (new Date).getTime())
                }), $(".password-login").on("click", function () {
                    var e = $("#username"),
                        o = $("#password"),
                        a = $("#captcha"),
                        n = $(".password-form");
                    if (!e.val() && !o.val()) return t.showAccountError("请输入账号和密码"), e.addClass("error"), void o.addClass("error");
                    if (!e.val()) return t.showAccountError("请输入账号"), void e.addClass("error");
                    if (e.removeClass("error"), !o.val()) return t.showAccountError("请输入密码"), void o.addClass("error");
                    if (o.removeClass("error"), $(".captcha-img-wrap").length) {
                        if (!a.val()) return t.showAccountError("请输入验证码"), void a.addClass("error");
                        a.removeClass("error")
                    }
                    t.hideAccountError(),
                        function () {
                            1 == $("#remember-account").prop("checked") ? r.set("username", $("#username").val()) : r.del("username");
                            $(".login-loading").removeClass("hide"),
                                n.find(".blank")[0].innerHTML = "",
                                n.find(".blank")[0].innerHTML = '<input class="hide" name="loginType" value="password">'

                        }()
                }), $(document).keyup(function (e) {
                    13 == e.keyCode && $(".form-content:visible").find(".login-btn").trigger("click")
                }), $(".mobile-login").on("click", function () {
                    var e;
                    c() && (((e = $("#mobile-captcha")).val() ? /^\d{6}$/.test(e.val()) ? (e.removeClass("error"), t.hideAccountError(), 1) : (t.showAccountError("请输入有效的验证码"), e.addClass("error"), 0) : (t.showAccountError("请输入验证码"), e.addClass("error"), 0)) && ($(".login-loading").removeClass("hide"), $(".mobile-form").find(".blank")[0].innerHTML = "", $(".mobile-form").find(".blank")[0].innerHTML = '<input class="hide" name="loginType" value="mobile">', $(".mobile-form").submit()))
                }), $(".CA-login-btn").on("click", function () {
                    var o = $("#ca-password");
                    if (e.certificateNum) {
                        if (!o.val()) return t.showCAError("请输入证书密码"), void o.addClass("error");
                        t.hideCAError(), o.removeClass("error"), $(".login-loading").removeClass("hide"), e.getCertificateInfo(o.val(), null, function (e) {
                            $(".CA-form").find(".blank")[0].innerHTML = "", $(".CA-form").find(".blank")[0].innerHTML = '<form class="hide ca-post-form" action="login" method="POST"><input class="hide" name="loginType" value="CA"><input class="hide" name="certRegNo" value=' + e.retvalue.certRegNo + "></form>", $(".ca-post-form").submit()
                        })
                    }
                });
                var o, n, i, s, l = $("#mobile-number");

                function c(e) {
                    var o = $(".get-captcha");
                    return l.val() ? /^(13|14|15|17|18)\d{9}$/i.test(l.val()) ? (l.removeClass("error"), t.hideAccountError(), o.removeClass("disabled"), !0) : ("blur" == e ? r("请输入有效的手机号") : l.val().length >= 11 ? r("请输入有效的手机号") : o.addClass("disabled"), !1) : (r("请输入用户名"), !1);

                    function r(e) {
                        t.showAccountError(e), o.addClass("disabled"), l.addClass("error")
                    }
                }

                o = navigator.appName, n = navigator.appVersion.split(";"), i = n[1] ? n[1].replace(/[ ]/g, "") : "", s = !1, "Netscape" !== o && "Microsoft Internet Explorer" !== o || "MSIE8.0" !== i && "MSIE7.0" !== i && "MSIE6.0" !== i || (s = !0), s && $(".pretty").remove(), l.on("input", c), l.on("blur", function () {
                    $(".account-form[data-type=mobile]").hasClass("hide") || c("blur")
                })
            }, refreshForm: function (e) {
                e.find(".login-input").removeClass("error"), e.find(".error-text").removeClass("error").find(".text").html("")
            }, initPopover: function () {
                var e = $(".forget-account"),
                    o = $(".remember-wrap"),
                    r = e.siblings(".popover-info").clone(!0).show(),
                    t = $(".remember-account-info").clone(!0).show();
                e.mouseenter(function () {
                    e.popover({
                        placement: "top",
                        html: !0,
                        content: r
                    }).popover("show")
                }).mouseleave(function () {
                    $(this).popover("destroy")
                })
            }, autoLogin: function () {
                var e = r.get("username");
                void 0 != e && "" != e && ($("#username").val(e), $("#remember-account").prop("checked", "checked"))
            }, loadingCA: function (e) {
                $(".CA-tab").attr("type", e), $(".CA-form").attr("data-type", e), "CA-loading" === e ? (t.hideCAError(), this.clientIsExist(), $(".ca-loading").removeClass("hide"), $(".loading-over").addClass("hide")) : ($(".ca-loading").addClass("hide"), $(".loading-over").removeClass("hide"))
            }, clientIsExist: function () {
                var e, o = this;

                function r() {
                    var e = $(".login-tab.active").attr("type");
                    $("#ca-account").parent(".input-wrap").removeClass("success"), $("#ca-account").val(""), "CA" !== e && "CA-loading" !== e || (a(), t.showCAError("UKEY客户端驱动未安装，请下载"))
                }

                function a() {
                    $(".ca-loading").addClass("hide"), $(".loading-over").removeClass("hide"), $(".CA-tab").attr("type", "CA"), $(".CA-form").attr("data-type", "CA")
                }

                this.certificateNum = 0, e = window.setTimeout(function () {
                    r()
                }, 5e3), $.ajax({
                    type: "get",
                    async: !1,
                    url: "#",
                    dataType: "jsonp",
                    timeout: 5e3,
                    jsonp: "callback",
                    jsonpCallback: "myjsonpcall",
                    success: function (n) {
                        0 == n.retcode ? (window.clearTimeout(e), window.setTimeout(function () {
                            a(), t.hideCAError();
                            var e = n.retvalue;
                            $("#ca-account").val("浙江CA-联连e照通驱动V" + e).parent(".input-wrap").addClass("success"), $.ajax({
                                type: "get",
                                async: !1,
                                url: "#",
                                dataType: "jsonp",
                                jsonp: "callback",
                                jsonpCallback: "myjsonpcall",
                                success: function (e) {
                                    $.ajax({
                                        type: "get",
                                        async: !1,
                                        url: "#",
                                        dataType: "jsonp",
                                        jsonp: "callback",
                                        jsonpCallback: "myjsonpcall",
                                        success: function (e) {
                                            if ("0" != e.retcode) t.showCAError("未检测到CA锁");
                                            else {
                                                var r = e.retvalue;
                                                r > 0 ? (t.hideCAError(), o.certificateNum = r) : t.showCAError("请插入CA锁")
                                            }
                                        },
                                        error: function () {
                                            t.showCAError("请插入CA锁")
                                        }
                                    })
                                }
                            })
                        }, 2e3)) : window.setTimeout(function () {
                            r()
                        }, 3e3)
                    }
                })
            }, getCertificateInfo: function (e, o, a) {
                var n = window.setTimeout(function () {
                    $(".login-loading").addClass("hide"), t.showCAError("获取证书信息失败")
                }, 1e4);
                $.ajax({
                    type: "get",
                    async: !1,
                    url: "#",
                    data: {
                        clientid: "zcy-client",
                        serverurl: "#",
                        signcontent: "1111111111111",
                        strcn: o || "ZJCA;ZJCA OCA1;ZJCA OCA2;CEGN SM2 Class 2 CA",
                        password: e
                    },
                    dataType: "jsonp",
                    jsonp: "callback",
                    jsonpCallback: "myjsonpcall",
                    success: function (e) {
                        var o;
                        if (window.clearTimeout(n), "0" != e.retcode) t.showCAError(e.errinfo || "登录失败，请重试"), $(".login-loading").addClass("hide");
                        else {
                            var i = e.retvalue,
                                s = i.certRegNo,
                                l = (o = (o = i.certName).replace(/\\/g, "%"), unescape(o));
                            r.set("certInfo", JSON.stringify({
                                unisicid: s,
                                unitName: l
                            })), a(e), t.hideCAError()
                        }
                    }
                })
            }, showAgreementModal: function () {
                if (!r.get("isAgree")) {
                    var o = new e("#agreement-modal");
                    o.show();
                    var t = 5,
                        a = window.setInterval(function () {
                            t--, $("#agreement-modal .second").html(t), t < 1 && ($("#agreement-modal #agree-btn").removeClass("disabled"), $("#agreement-modal .count-down").addClass("hide"), window.clearInterval(a))
                        }, 1e3);
                    $("#agree-btn").on("click", function () {
                        $(this).hasClass("disabled") || (r.set("isAgree", !0), o.close())
                    }), $("#disagree-btn").on("click", function () {
                        r.set("isAgree", !1), o.close(), window.location.href = "#"
                    })
                }
            }, renderError: function () {
                var e = $(".login-type-res").html(),
                    o = $(".error-info-res").html();
                o ? "CA" !== e ? ("mobile" === e ? ($(".change-login-type[type=mobile]").trigger("click"), o && t.showAccountError(o)) : o && t.showAccountError(o), $(".account-tab").addClass("active")) : ($(".CA-form").removeClass("hide").attr("data-type", "CA").siblings(".form-content").addClass("hide"), $(".ca-loading").addClass("hide"), $(".loading-over").removeClass("hide"), $(".CA-tab").attr("type", "CA").addClass("active"), o && t.showCAError(o)) : $(".account-tab").addClass("active")
            }
        };
    n.bindEvents(), n.renderError(), n.autoLogin(), n.initPopover()
});